package com.itheima.test;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @Author 王加鑫
 * @Date 2021/4/29 19:23
 * @Version 1.0
 */
public class StudentManager {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //创建一个集合作为容器,来存储学生信息
        ArrayList<Student> list = new ArrayList<>();
        for(;;){
            System.out.println("----------欢迎使用学生管理系统----------");
            System.out.println("1 添加学生信息");
            System.out.println("2 删除学生信息");
            System.out.println("3 修改学生信息");
            System.out.println("4 查询学生信息");
            System.out.println("5 退出系统");
            System.out.println("请秀出您的操作:");
            String s = sc.next();
            if("1".equals(s)){
                addStudent(list);
            }else if("2".equals(s)){
                deleteStudent(list);
            }else if("3".equals(s)){
                updateStudent(list);
            }else if("4".equals(s)){
                selectStudent(list);
            }else if("5".equals(s)){
                System.out.println("退出系统");
                break;
            }else {
                System.out.println("您的输入有误,请重新输入:");
            }
        }
    }

    /*
    修改学生
     */
    public static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要修改的学生id:");
        String sId = sc.next();
        int index = getIndex(list,sId);
        if(index == -1){
            System.out.println("学生信息不符,请重新输入:");
        }else {
            System.out.println("请输入要修改的学生姓名:");
            String name = sc.next();
            System.out.println("请输入要修改的学生年龄:");
            int age = sc.nextInt();
            System.out.println("请输入要修改的学生生日:");
            String birth = sc.next();
            Student student = new Student(sId,name,age,birth);
            //集合方法,根据指定索引修改值
            list.set(index,student);
            System.out.println("修改成功!");
        }


    }

    /*
    删除学生信息
     */
    public static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入您要删除的学生id:");
        String sId = sc.next();
        //判断索引是否在集合中
        int index = getIndex(list,sId);
        if(index == -1){
            System.out.println("学生信息不符,请重新输入:");
        }else {
            list.remove(index);
            System.out.println("删除成功!");
        }

    }

    /*
    查询学生信息
     */
    public static void selectStudent(ArrayList<Student> list) {
        if(list.size()!=0){
            System.out.println("学生id\t\t学生姓名\t\t学生年龄\t\t学生生日");
            for (int i = 0; i < list.size(); i++) {
                System.out.println(list.get(i).getsId() + "\t\t"
                        + list.get(i).getName() + "\t\t" +list.get(i).getAge() + "\t\t" + list.get(i).getBirth());
            }
        }else {
            System.out.println("系统还没有输入数据,请输入数据再查询!");
        }
    }

    /*
    添加学生信息
     */
    public static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生学号:");
        while (true){
            String id = sc.next();
            //当集合不存在该索引时,添加索引才生效
            if(getIndex(list,id) != -1){
                System.out.println("学号重复,请重新输入:");
            }else {
                System.out.println("请输入学生姓名:");
                String name = sc.next();
                System.out.println("请输入学生年龄:");
                int age = sc.nextInt();
                System.out.println("请输入学生生日");
                String birth = sc.next();
                list.add(new Student(id,name,age,birth));
                System.out.println("添加成功!");
                break;
            }
        }
    }

    /*
    判断键盘录入的学生id是否在集合容器中
    是返回该id在集合中的索引值
    不是返回-1
     */
    private static int getIndex(ArrayList<Student> list,String sid) {
        //标记为 假设索引不存在,值为-1
        int index = -1;
        for (int i = 0; i < list.size(); i++) {
            //如果遍历数组获取的id和sid一样,那么返回该id的索引
            if(list.get(i).getsId().equals(sid)){
                index = i;
            }
        }
        return index;
    }
}
